﻿using System;
using System.Linq.Expressions;
using System.Linq;
using System.Runtime.Serialization;

namespace ExpressionSerialization
{
  [DataContract]
  public class XParameterExpression : XExpression
  {

    [DataMember]
    public string ParameterName { get; set; }

    public override void FromExpression(Expression source_)
    {
      base.FromExpression(source_);
      ParameterName = (source_ as ParameterExpression).Name;
    }

    public override Expression ToExpression(Type dataContract_, System.Collections.Generic.List<ParameterExpression> parameters_)
    {
      ParameterExpression parameter = parameters_.FirstOrDefault(p => p.Name == ParameterName );
      if(parameter==null) {
        parameter = Expression.Parameter(GetExpressionType(dataContract_), ParameterName);
        parameters_.Add(parameter);
      }
      return parameter;
    }
  }
}
